﻿/*
 * 文件名称：
 * 程序员：wangyun
 * 创建时间：2008/07/15
 * 
 * 功能说明：模板修改备份
 * 
 * 修改人员：
 * 
 * 修改日期：
 * 
 * 修改记录：
 */

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections;

public class FileBackupHelper
{
    public FileBackupHelper()
    {
    }

    /// <summary>
    /// 返回指定文件的备份列表
    /// </summary>
    /// <param name="_page">Page</param>
    /// <param name="filePath">指定文件的路径</param>
    /// <returns>备份列表</returns>
    public static ArrayList ListHistory(System.Web.UI.Page _page, string filePath)
    {
        DataTable dt = FileSystemObject.GetDirectoryInfos(_page.Server.MapPath("~/UploadFiles/template/bak/"), FsoMethod.File);
        ArrayList namelist = new ArrayList();
        string fileName = Path.GetFileNameWithoutExtension(filePath);
        foreach (DataRow dr in dt.Rows)
        {
            if (dr["name"].ToString().Split('_')[0].ToString() == fileName)
            {
                namelist.Add(dr["name"].ToString());
            }
        }
        return namelist;
    }

    /// <summary>
    /// 备份
    /// </summary>
    /// <param name="_page">Page</param>
    /// <param name="filePath">源文件路径</param>
    /// <param name="toPath">备份文件路径</param>
    /// <example>
    /// FileBackupHelper.Backup(Page, 
    /// Server.MapPath("~/Templates/hengda/group/profile.html")  ,
    /// Server.MapPath("~/UploadFiles/template/bak/"));
    /// </example>
    public static bool Backup(System.Web.UI.Page _page, string filePath, string toPath)
    {
        try
        {
            StreamReader sr = new StreamReader(filePath);
            string content = sr.ReadToEnd();
            string fileName = Path.GetFileNameWithoutExtension(filePath);
            string fileEx = Path.GetExtension(filePath);
            string date = DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒");
            string newFileName = fileName + "_" + date + fileEx;
            WriteFile(toPath + newFileName, content);         
            sr.Close();
            return true;
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
    /// 还原
    /// </summary>
    /// <param name="_page">Page</param>
    /// <param name="filePath">备份文件路径</param>
    /// <param name="toPath">还原路径</param>
    /// <example>
    /// FileBackupHelper.Restore(Page,
    /// Server.MapPath("~/Whir_System/file/bak/profile_2008-07-21-09-51-19-3281.html"),
    /// Server.MapPath("~/Whir_System/file/"));
    /// </example>
    public static bool Restore(System.Web.UI.Page _page, string filePath, string toPath)
    {
        try
        {
            StreamReader sr = new StreamReader(filePath);
            string content = sr.ReadToEnd();
            string fileName = Path.GetFileNameWithoutExtension(filePath).Split('_')[0].ToString();
            string fileEx = Path.GetExtension(filePath);
            string newFileName = fileName + fileEx;
            WriteFile(toPath + newFileName, content);
            sr.Close();
            return true;
        }
        catch
        {
            return false;
        }
    }

    private static void WriteFile(string filepath, string filecontent)
    {
        FileInfo file = new FileInfo(filepath);
        if (!Directory.Exists(file.DirectoryName))
        {
            Directory.CreateDirectory(file.DirectoryName);
        }

        using (FileStream fs = new FileStream(filepath, FileMode.Create, FileAccess.Write))
        {
            using (StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8))
            {
                sw.Write(filecontent);
            }
        }        
    }
}